import { useModel } from "umi";
import { useReactive } from "ahooks";

const useInitialization = () => {
    const { remoteData } = useModel("global");
    const { baseInfo } = useModel("person.info");
    const { selectedGateway } = useModel("gateway.info");

    const state = useReactive({
        loading: false,
    });

    const personInitialization = async (callback) => {
        if (!selectedGateway.url) return;
        state.loading = true;
        let res = await remoteData({
            object: "multi-person-gateway",
            p2pNodeUuid: selectedGateway.uuid,
            serviceUrl: selectedGateway.url,
            serviceName: "/person/initialization",
            params: {
                uuid: baseInfo.uuid,
                gatewayUuid: selectedGateway.uuid,
                pmUuid: baseInfo.uuid,
                pmName: baseInfo.realName,
            },
        });
        callback && callback(res);
        state.loading = false;
    };

    return { ...state, personInitialization };
};

export default useInitialization;
